package eu.ahsx.limboa.utils
{
	/**
	 * 	Utility method related to xml management
	 * 
	 * 	@author Masy Alexandre
	 * */
	public class XMLUtils
	{
		/**
		 * 	Find and return a key in a xml. The key is a concatenation of node separated by "."
		 * 	
		 * 	@param	key String	Key to find
		 * 	@param	xml XML		XML data to search in
		 * 	@return String 
		 * */
		public static function getOption(key:String, xml:XML):*
		{
			var ret:*;
			
			var list:Array = key.split(".");
			var x:XMLList = xml.children();
			var k:String;
			var e:XML;
			var found:Boolean = false;
			for ( var i:int = 0; i<list.length; i++ )
			{
				k = list[i];
				found = false;
				
				for each (e in x)
				{
					if( e.name() == k )
					{
						x = e.children();
						found = true;
						break;
					}
				}
				
				if (x.length() == 0)
					break;
				
				if (i == list.length-1)
					ret = x.toString();
				
				if (!found)
				{
					ret = null;
					break;
				}
			}
			
			return ret;
		}
	}
}